

'''
-- 创建设备类型表
CREATE TABLE device_types (
    id SERIAL PRIMARY KEY,
    device_type_name VARCHAR(50) NOT NULL UNIQUE,
    description TEXT
);

SELECT SETVAL('device_types_id_seq',max(id),TRUE) FROM device_types;
'''

from . import logger
import psycopg2

class DeviceTypesTable:

    def __init__(self, connection, cursor):

        self.connection = connection
        self.cursor = cursor


    def create_device_type(self, device_type_name, description):
        try:
            self.cursor.execute("INSERT INTO device_types (device_type_name, description) VALUES (%s, %s)", (device_type_name, description))
            logger.info(f"Device type {device_type_name} created successfully.")
            return True
        except psycopg2.errors.UniqueViolation as e:
            logger.warning(f"Device type {device_type_name} already exists.")
            return True

        except Exception as e:
            logger.error(f"Error creating device type {device_type_name}: {e}")
            return False

    def get_all_device_types(self):
        try:
            self.cursor.execute("SELECT * FROM device_types")
            device_types = self.cursor.fetchall()
            logger.info(f"Got all device types successfully.")
            return device_types
        except Exception as e:
            logger.error(f"Error getting all device types: {e}")
            return None

    def get_device_type_by_id(self, device_type_id):
        try:
            self.cursor.execute("SELECT * FROM device_types WHERE id = %s", (device_type_id,))
            device_type = self.cursor.fetchone()
            logger.info(f"Got device type {device_type_id} successfully.")
            return device_type
        except Exception as e:
            logger.error(f"Error getting device type {device_type_id}: {e}")
            return None

    def get_device_type_by_name(self, device_type_name):
        try:
            self.cursor.execute("SELECT * FROM device_types WHERE device_type_name = %s", (device_type_name,))
            device_type = self.cursor.fetchone()
            logger.info(f"Got device type {device_type_name} successfully.")
            return device_type
        except Exception as e:
            logger.error(f"Error getting device type {device_type_name}: {e}")
            return None

    def update_device_type(self, device_type_id, device_type_name, description):
        try:
            self.cursor.execute("UPDATE device_types SET device_type_name = %s, description = %s WHERE id = %s", (device_type_name, description, device_type_id))
            logger.info(f"Device type {device_type_id} updated successfully.")
            return True
        except Exception as e:
            logger.error(f"Error updating device type {device_type_id}: {e}")
            return False

    def delete_device_type(self, device_type_id):
        try:
            self.cursor.execute("DELETE FROM device_types WHERE id = %s", (device_type_id,))
            logger.info(f"Device type {device_type_id} deleted successfully.")
            return True
        except Exception as e:
            logger.error(f"Error deleting device type {device_type_id}: {e}")
            return False

    